package cn.bdqn.controller.questionBank;

import cn.bdqn.dto.LoginUser;
import cn.bdqn.dto.questionBank.subject.SubjectDataPageDTO;
import cn.bdqn.dto.questionBank.subject.SubjectOneDataPageDTO;
import cn.bdqn.dto.questionBank.subject.SubjectPageDTO;
import cn.bdqn.dto.questionBank.subject.SubjectUpdateDTO;
import cn.bdqn.service.questionBank.AccountTypeService;
import cn.bdqn.service.questionBank.SubjectService;
import cn.bdqn.service.questionBank.SubjectTypeService;
import cn.bdqn.utils.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;
    @Autowired
    private AccountTypeService accountTypeService;

    @Autowired
    private SubjectTypeService subjectTypeService;

    /**
     * 根据条件查询题目信息
     * @param subjectTypeId
     * @param accountTypeId
     * @param status
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/selectSubject")
    public Json selectSubject(Integer subjectTypeId,Integer accountTypeId,Integer status,Integer pageNumber,Integer pageSize){
        return subjectService.selectSubject(subjectTypeId,accountTypeId,status,pageNumber,pageSize);
    }

    /**
     * 初始化subject页面
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/initializationSubjectPage")
    public Json initializationSubjectPage(Integer subjectTypeId,Integer accountTypeId,Integer status,Integer pageNumber,Integer pageSize){
        Json json = subjectService.selectSubject(subjectTypeId,accountTypeId,status,pageNumber,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("subject",json.getData());
        map.put("account",accountTypeService.selectIndustryAccountType());
        map.put("subject_type",subjectTypeService.selectAllSubjectType().getData());
        return Json.ok(map);
    }
    /**
     * 批量添加题目
     * @param httpServletRequest
     * @param subjectPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/insertSubjectList")
    public Json insertSubjectList(HttpServletRequest httpServletRequest,
                                  @RequestBody SubjectDataPageDTO subjectPageDTO){

        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();

            return subjectService.insertSubjectList(subjectPageDTO.getSubjectDTOS(),subjectPageDTO.getAccountTypeId(),userid,subjectPageDTO.getSubjectPageDTO());
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 添加一条数据
     * @param httpServletRequest
     * @param subjectOneDataPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/insertOneSubject")
    public Json insertOneSubject(HttpServletRequest httpServletRequest, @RequestBody SubjectOneDataPageDTO subjectOneDataPageDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();

            return subjectService.insertOneSubject(subjectOneDataPageDTO.getSubjectDTO(),subjectOneDataPageDTO.getAccountTypeId(),userid,subjectOneDataPageDTO.getSubjectPageDTO());
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 删除一条数据
     * @param servletRequest
     * @param subjectPageDTO
     * @param subjectId
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @DeleteMapping("/deleteOneSubject")
    public Json deleteOneSubject(HttpServletRequest servletRequest, SubjectPageDTO subjectPageDTO, Integer subjectId){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return subjectService.deleteOneSubject(subjectId,userid,subjectPageDTO);
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 更新一条题目
     * @param servletRequest
     * @param subjectUpdateDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PutMapping("/updateOneSubject")
    public Json updateOneSubject(HttpServletRequest servletRequest,@RequestBody SubjectUpdateDTO subjectUpdateDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();

        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return subjectService.updateOneSubject(subjectUpdateDTO.getSubject(),subjectUpdateDTO.getOperationRecordRemarks(),subjectUpdateDTO.getAccountTypeId(),userid,subjectUpdateDTO.getSubjectPageDTO());

        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 发布一条题目
     * @param servletRequest
     * @param subjectUpdateDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PutMapping("/statusOneSubject")
    public Json statusOneSubject(HttpServletRequest servletRequest,@RequestBody SubjectUpdateDTO subjectUpdateDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return subjectService.statusOneSubject(userid,subjectUpdateDTO.getSubject().getSubjectId(),subjectUpdateDTO.getSubjectPageDTO());

        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param servletRequest
     * @param subjectIdList
     * @param subjectPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @DeleteMapping("/deleteListSubject")
    public Json deleteListSubject(HttpServletRequest servletRequest,@RequestParam String subjectIdList,SubjectPageDTO subjectPageDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        List<Integer> list = new ArrayList<>();
        if(subjectIdList != null && !"".equals(subjectIdList)){
            String[] integers = subjectIdList.split(",");
            for(String a : integers){
                list.add(Integer.parseInt(a));
            }
        }



        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return subjectService.deleteListSubject(userid,list,subjectPageDTO);

        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 批量发布
     * @param servletRequest
     * @param subjectIdList
     * @param subjectPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/statusListSubject")
    public Json statusListSubject(HttpServletRequest servletRequest,@RequestParam String subjectIdList,SubjectPageDTO subjectPageDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        List<Integer> list = new ArrayList<>();
        if(subjectIdList != null && !"".equals(subjectIdList)){
            for(String a : subjectIdList.split(",")){
                list.add(Integer.parseInt(a));
            }
        }

        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return subjectService.statusListSubject(userid,list,subjectPageDTO);

        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 用户选择题目
     * @param subjectPageDTO
     * @param subjectId
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/userSelectSubject")
    public Json userSelectSubject(SubjectPageDTO subjectPageDTO,@RequestParam String subjectId){
        List<Integer> list = new ArrayList<>();
        if(!"".equals(subjectId) && subjectId != null){
            for(String a : subjectId.split(",")){
                list.add(Integer.parseInt(a));
            }
        }
        return subjectService.userSelectSubject(subjectPageDTO,list);
    }
}
